package hu.rsc.forbiddenwords.client;

import hu.rsc.forbiddenwords.client.gui.application.ApplicationPanel;
import hu.rsc.forbiddenwords.client.gui.entry.EntryPanel;
import hu.rsc.forbiddenwords.client.gui.language.LanguagePanel;
import hu.rsc.forbiddenwords.client.util.SimpleCallback;
import hu.rsc.forbiddenwords.shared.ApplicationDTO;
import hu.rsc.forbiddenwords.shared.EntryDTO;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.TextArea;
import com.google.gwt.user.client.ui.VerticalPanel;

/**
 * Entry point classes define <code>onModuleLoad()</code>.
 */
public class Forbiddenwords implements EntryPoint {
    /**
     * Create a remote service proxy to talk to the server-side Greeting service.
     */
    private static final ForbiddenWordsServiceAsync service = GWT.create(ForbiddenWordsService.class);

    private static ApplicationDTO selectedApplication = new ApplicationDTO(0l);

    private static ApplicationPanel applicationPanel;

    private static EntryPanel entryPanel;

    public static ApplicationDTO getSelectedApplication() {
	return selectedApplication;
    }

    /**
     * The message displayed to the user when the server cannot be reached or returns an error.
     */

    public static ForbiddenWordsServiceAsync getService() {
	return service;
    }

    public static void setSelectedApplication(ApplicationDTO application) {
	selectedApplication = application;
	entryPanel.refresh(new EntryDTO(0l));
    }

    /**
     * This is the entry point method.
     */
    @Override
    public void onModuleLoad() {
	VerticalPanel vp = new VerticalPanel();

	Button settingsBt = new Button("Languages");
	settingsBt.addClickHandler(new ClickHandler() {

	    @Override
	    public void onClick(ClickEvent event) {
		DialogBox box = new DialogBox(true);
		box.setText("Languages");
		box.add(new LanguagePanel());
		box.center();
	    }
	});
	vp.add(settingsBt);

	HorizontalPanel hp = new HorizontalPanel();
	vp.add(hp);

	applicationPanel = new ApplicationPanel();
	hp.add(applicationPanel);

	entryPanel = new EntryPanel();
	hp.add(entryPanel);

	final TextArea ta = new TextArea();
	ta.setWidth("100%");
	ta.setHeight("300px");
	vp.add(ta);

	Button bt = new Button("Send");
	bt.addClickHandler(new ClickHandler() {

	    @Override
	    public void onClick(ClickEvent event) {
		Forbiddenwords.this.service.filter(ta.getValue(), new SimpleCallback<String>() {

		    @Override
		    public void onSuccess(String result) {
			ta.setText(ta.getText() + "\r\n" + result);
		    }
		});
	    }
	});
	vp.add(bt);

	RootPanel.get().add(vp);
    }
}
